package com.netease.xone.view;


import Utils.StackTraceUtil;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;

import com.netease.log.NTLog;
import com.netease.xone.activity.ActivityWelcome;
import com.netease.xone.adapter.AdapterBaseFragmentPager;
import com.netease.xone.adapter.AdapterMainFragmentPager;
import com.netease.xone.fragment.FragmentTouTiao;
import com.netease.xone.fragment.IFragmentMain;
import com.netease.xone.widget.tabview.AbsTabView.OnTabSelectListener;
import com.netease.xone.widget.tabview.MainTab;

public class MainContainerView extends ContainerView implements OnPageChangeListener {
    private String TAG = MainContainerView.class.getSimpleName();

    private IFragmentMain mIFragmentMain;

    public MainContainerView(Context context, IFragmentMain iFragmentMain) {
        super(context);
        mIFragmentMain = iFragmentMain;
    }

    @Override
    protected ViewPager onInitPager(Object obj) {
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " obj=" + (null == obj ?  "null": obj.toString()));
        CustomViewPager viewPager = null;
        Context context = null;

        context = getContext();
        AdapterMainFragmentPager adapter = null;
        if (context instanceof FragmentActivity) {
            FragmentActivity activity = (FragmentActivity)context;
            FragmentManager fm = activity.getSupportFragmentManager();
            if (null == obj) {
                adapter = new AdapterMainFragmentPager(fm, context);
            } else {
                adapter = (AdapterMainFragmentPager)obj;
            }
        }

        viewPager = new CustomViewPager(context);
        viewPager.setAdapter(adapter);
        viewPager.setAllowedScrolling(false);
        return viewPager;
    }

    @Override
    protected ViewGroup onInitBottom(Object obj) {
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " obj=" + (null == obj ?  "null": obj.toString()));
        return new MainTab(getContext());
    }

    @Override
    protected void setPagerAndBottom(ViewPager viewPager, ViewGroup bottom) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        if (null == viewPager || null == bottom) {
            return;
        }
        viewPager.setOnPageChangeListener(this);
        viewPager.setOffscreenPageLimit(5);
        mViewPager = viewPager;
        mBottom = bottom;

        ((MainTab)mBottom).setOnTabSelectListener(new OnTabSelectListener() {

            @Override
            public void onTabSelected(View tabView, int index) {
                NTLog.d(TAG, StackTraceUtil.getMethodName() + " index=" + index);
                if (null != mViewPager && null != mViewPager.getAdapter()) {
                    int count = mViewPager.getAdapter().getCount();
                    if (index < 0 && index >= count) {
                        return;
                    }
                    mViewPager.setCurrentItem(index, false);
                }
            }

            @Override
            public void onTabReSelected(View tabView, int index) {
                if (null != mIFragmentMain) {
                    mIFragmentMain.onPageReSelected(index);
                }
            }
        });
        if (null != mIFragmentMain) {
            mIFragmentMain.onPageSelected(0);
        }
    }

    private int mDeltaX = 0;
    @Override
    public void onPageScrollStateChanged(int arg0) {
        /**
         * ��页��头��         */
        if (arg0 == ViewPager.SCROLL_STATE_IDLE) {
            if (0 == mDeltaX && mViewPager.getCurrentItem() == 0) {
                ActivityWelcome.launch(getContext(), FragmentTouTiao.MODE_FINISH);
            }
        } else if (arg0 == ViewPager.SCROLL_STATE_DRAGGING) {
            mDeltaX = 0;
        } else {

        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        mDeltaX += arg2;
    }

    @Override
    public void onPageSelected(int index) {
        NTLog.d(TAG, StackTraceUtil.getMethodName() + " index=" + index);
        if(mBottom instanceof  MainTab){
            ((MainTab)mBottom).setCurrentTab(index);
        }

        if (null != mIFragmentMain) {
            mIFragmentMain.onPageSelected(index);
        }
    }

    public void clear() {
        if (null != mViewPager && null != mViewPager.getAdapter()) {
            if (mViewPager.getAdapter() instanceof AdapterBaseFragmentPager) {
                ((AdapterBaseFragmentPager)mViewPager.getAdapter()).clear();
            }
        }
        mViewPager = null;
        mBottom = null;
        mIFragmentMain = null;
    }
}
